/*     */ package com.zimbra.cs.server;
/*     */ 
/*     */ import java.nio.ByteBuffer;
/*     */ import org.apache.mina.core.buffer.IoBuffer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class NioUtil
/*     */ {
/*     */   public static final byte CR = 13;
/*     */   public static final byte LF = 10;
/*     */   public static final byte DOT = 46;
/*     */   public static final int INITIAL_CAPACITY = 32;
/*     */   
/*     */   public static ByteBuffer expand(ByteBuffer bb, int minSize, int maxSize)
/*     */   {
/*  41 */     if ((maxSize != -1) && (maxSize < minSize)) {
/*  42 */       throw new IllegalArgumentException("maxSize < minSize");
/*     */     }
/*  44 */     if (bb == null) {
/*  45 */       int size = Math.max(minSize, 32);
/*  46 */       if ((maxSize != -1) && (size > maxSize)) size = maxSize;
/*  47 */       return ByteBuffer.allocate(size);
/*     */     }
/*  49 */     if (bb.remaining() >= minSize) return bb;
/*  50 */     int capacity = Math.max(bb.capacity() * 3 / 2 + 1, bb.position() + minSize);
/*     */     
/*  52 */     if (maxSize != -1) {
/*  53 */       capacity = Math.max(capacity, bb.position() + maxSize);
/*     */     }
/*  55 */     ByteBuffer tmp = ByteBuffer.allocate(capacity);
/*  56 */     bb.flip();
/*  57 */     return tmp.put(bb);
/*     */   }
/*     */   
/*     */   public static ByteBuffer expand(ByteBuffer bb, int minSize) {
/*  61 */     return expand(bb, minSize, -1);
/*     */   }
/*     */   
/*     */   public static String toAsciiString(ByteBuffer bb) {
/*  65 */     int len = bb.remaining();
/*  66 */     char[] cs = new char[len];
/*  67 */     for (int i = 0; i < len; i++) {
/*  68 */       cs[i] = ((char)(bb.get(i) & 0xFF));
/*     */     }
/*  70 */     return new String(cs);
/*     */   }
/*     */   
/*     */   public static ByteBuffer toAsciiBytes(String s) {
/*  74 */     return put(ByteBuffer.allocate(s.length()), s);
/*     */   }
/*     */   
/*     */   public static ByteBuffer put(ByteBuffer bb, String s) {
/*  78 */     bb = expand(bb, s.length());
/*  79 */     for (int i = 0; i < s.length(); i++) {
/*  80 */       bb.put(i, (byte)s.charAt(i));
/*     */     }
/*  82 */     return bb;
/*     */   }
/*     */   
/*     */   public static byte[] getBytes(ByteBuffer bb) {
/*  86 */     if ((bb.hasArray()) && (bb.arrayOffset() == 0) && (bb.position() == 0)) {
/*  87 */       byte[] b = bb.array();
/*  88 */       if (b.length == bb.limit()) return b;
/*     */     }
/*  90 */     byte[] b = new byte[bb.limit() - bb.position()];
/*  91 */     bb.duplicate().get(b);
/*  92 */     return b;
/*     */   }
/*     */   
/*     */   public static IoBuffer toIoBuffer(ByteBuffer bb) {
/*  96 */     return IoBuffer.wrap(bb);
/*     */   }
/*     */   
/*     */   public static String toHexString(ByteBuffer bb) {
/* 100 */     return appendHex(new StringBuilder(), bb).toString();
/*     */   }
/*     */   
/*     */   public static StringBuilder appendHex(StringBuilder sb, ByteBuffer bb) {
/* 104 */     int limit = bb.limit();
/* 105 */     for (int i = bb.position(); i < limit; i++) {
/* 106 */       int c = bb.get(i) & 0xFF;
/* 107 */       sb.append(Character.forDigit(c >> 4, 16));
/* 108 */       sb.append(Character.forDigit(c & 0xF, 16));
/* 109 */       if (i < limit - 1) sb.append(' ');
/*     */     }
/* 111 */     return sb;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/server/NioUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */